package com.cqu.lpss.controller;

import com.cqu.lpss.pojo.*;
import com.cqu.lpss.service.impl.ProfessorsImpl;
import com.cqu.lpss.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

@RestController
@Slf4j
@RequestMapping("/professors")
public class ProfessorsController {
    @Autowired
    ProfessorsImpl professorsimpl;
    @PostMapping("/login")
    public Result login(@RequestBody Professors professor){
        log.info("参赛人员登录：{}",professor);
        Professors loginprof = professorsimpl.login(professor);
        //登录成功生成jwt令牌
        if(loginprof!=null){
            HashMap<String, Object> claims = new HashMap<>();
            claims.put("userIndex",loginprof.getProIndex());
            claims.put("userId"   ,loginprof.getProId());
            claims.put("userName",loginprof.getProName());
            claims.put("userClass",loginprof.getProClass());
            claims.put("roleName","professor");
            String jwt = JwtUtils.generateJwt(claims);
            return Result.success(jwt);
        }
        //登录失败，返回错误信息
        return Result.erro("身份证或密码错误");
    }
    @PostMapping
    public Result list(
            @RequestParam(defaultValue = "1") int currentPage, // 当前页码
            @RequestParam(defaultValue = "10") int pageSize, // 每页显示数量
            String proId,
            String proName,
            Integer proLowAge,
            Integer proHighAge,
            Integer proGender,
            String proEmail,
            String proPhone,
            Integer proAuth,
            Integer proClass

    ){
        log.info("params:{}"+proEmail);
        PageBean<Professors> pageBean =professorsimpl.list(currentPage, pageSize, proId, proName, proLowAge,
                proHighAge, proGender, proEmail, proPhone, proAuth, proClass);
        return Result.success(pageBean);
    }
    @PostMapping("/danger_delete")
    public Result danger_delete(@RequestParam String UserId,@RequestParam String UserName,@RequestParam String UserEnterId,@RequestParam Integer deleteproIndex){
//        Professors prof=professorsimpl.findById(proId);
//       undo： 把这个输入的id存储到危险操作记录中
        if(UserId.equals(UserEnterId)){
            int ans = professorsimpl.danger_delete(UserId, UserName, deleteproIndex);
            if(ans==1){
                return Result.success();
            }else{
                return Result.erro("DELETE_FAILE");
            }

        }else{
            return Result.erro("ENTERID_NAT_MATCH");
        }
    }
    @PostMapping("/danger_add")
    public Result danger_add(@RequestBody DangerPostData<Professors> dangerPostData){
        log.info("addpro:{}",dangerPostData);
        String UserId = dangerPostData.getUserId();
        String UserName = dangerPostData.getUserName();
        String UserEnterId = dangerPostData.getUserEnterId();
        Professors addProfessor = dangerPostData.getPostData();
        if(UserId.equals(UserEnterId)){
            int ans = professorsimpl.danger_add(UserId,UserName,addProfessor);
            if(ans==1){
                return Result.success();
            }else if(ans==-1){
                return Result.erro("ID_duplicate");
            }else{
                return Result.erro("ADD_FAILE");
            }

        }else{
            return Result.erro("ENTERID_NAT_MATCH");
        }
    }
    @PostMapping("/danger_edit")
    public Result danger_edit(@RequestBody DangerPostData<Professors> dangerPostData){
        log.info("edit_pro:{}",dangerPostData);
        String UserId = dangerPostData.getUserId();
        String UserName = dangerPostData.getUserName();
        String UserEnterId = dangerPostData.getUserEnterId();
        Professors editProfessor = dangerPostData.getPostData();
        int ans = professorsimpl.danger_edit(UserId,UserName,editProfessor);
        if(ans==1){
            return Result.success();
        }else{
            return Result.erro("EDIT_FAILE");
        }

    }
    @GetMapping("/findById")
    public Result findById(@RequestParam String userId){
        Professors professors = professorsimpl.findById(userId);
        if(professors!=null){
            return Result.success(professors);
        }else {
            return Result.erro("未知错误");
        }
    }
    @PostMapping("/PersonCenter_upload")
    public Result PersonCenter_upload(@RequestParam String newPassword,@RequestParam String phone,@RequestParam String email ,@RequestParam String id){
        int ans = professorsimpl.PersonCenter_upload(newPassword, phone, email, id);
        if(ans!=0){
            return  Result.success();
        }
        else {
            return Result.erro("信息更新失败，请联系管理员");
        }
    }
}
